-- Program: NativeUITest.lua
-- Author: Mikael Kindborg (adapted from the <insert name> MoSync exampel program)
-- Description: This program shows how to use native widgets with Lua.

----------------------------------------------
-- Support for simple widget event handling --
----------------------------------------------

-- Table that contains widget -> fuction pairs.
gWidgetEventTable = {}

-- Function that initializes the event handler.
function initializeWidgetEventHandler()
	-- Widget events are sent as custom events.
	System.onCustomEvent(function(event)
		-- Is this a widget event?
  		if EVENT_TYPE_WIDGET == maEventGetType(event) then
  			-- Get the widget handle of the event.
  			local widget = getWidgetHandle(event)
  			-- Is there a function registered for this widget?
  			if nil ~= gWidgetEventTable[widget] then 
  				-- Yes there is, call the function.
  				gWidgetEventTable[widget](event)
  			end
		end
	end)
end

-- Helper function that returns the widget handle of an event.
function getWidgetHandle(event)
  local widgetEvent = maEventGetData(event)
  return maBufferGetInt(widgetEvent, 1)
end

-- Add an event function to a widget.
function addWidgetEvent(widget, eventFunction)
	gWidgetEventTable[widget] = eventFunction
end

----------------------------------------------
-- End of widget event handling support     --
----------------------------------------------

function createListScreen()
	local listScreen = maWidgetCreate("Screen")
	maWidgetSetProperty(listScreen, "title", "List")
--	System.downloadImage(
--		"http://findicons.com/files/icons/974/glyphish/23/83_calendar.png",
--		function (image)
--			--maWidgetSetProperty( listScreen, "icon", tonumber(image) )
--			System.log("Image handle: " .. image)
--		end)

	local list = maWidgetCreate("ListView")
	maWidgetSetProperty(list, "width", "-1")
	maWidgetSetProperty(list, "height", "-1")
	
	for i = 0, 100, 1 do
		local listItem = maWidgetCreate("ListViewItem")
		maWidgetSetProperty(listItem, "text", "Click me")
		--maWidgetSetPropertyInt( listItem, "icon", R_ICON );
		maWidgetAddChild(list, listItem)
	end

	maWidgetAddChild(listScreen, list)

	return listScreen
end

function createWebScreen()
	local webScreen = maWidgetCreate( "Screen" );
	maWidgetSetProperty( webScreen, "title", "Web" );
	--maWidgetSetPropertyInt( webScreen, "icon", R_TV );

	webView = maWidgetCreate( "WebView" );
	maWidgetSetProperty( webView, "width", "-1" );
	maWidgetSetProperty( webView, "height", "-1" );
	maWidgetSetProperty( webView, "url", "http://www.google.se/" );

	maWidgetAddChild( webScreen, webView );

	return webScreen;
end

function createEditBoxScreen()
	local screen = maWidgetCreate( "Screen" );
	maWidgetSetProperty( screen, "title", "Edit" );

	--maWidgetSetPropertyInt( screen, "icon", icon );

	local textView = maWidgetCreate( "EditBox" );
--	maWidgetSetProperty( textView, "text", text );
	maWidgetSetProperty( textView, "width", "-1" );
	maWidgetSetProperty( textView, "height", "-2" );

	addWidgetEvent(textView, function(event)
       	maWidgetSetProperty(textView, "showKeyboard", "false")
       	local text = "";
       	maWidgetGetProperty(textView, "text", text, 256);
       	maMessageBox("Hello", text)
	end)
	
	maWidgetAddChild(screen, textView);

	return screen;
end

initializeWidgetEventHandler()

tabScreen = maWidgetCreate( "TabScreen")

listScreen = createListScreen( );
maWidgetAddChild( tabScreen, listScreen );

webScreen = createWebScreen( );
maWidgetAddChild( tabScreen, webScreen );

editScreen = createEditBoxScreen( );
maWidgetAddChild( tabScreen, editScreen );

maWidgetScreenShow( tabScreen );
